package ltd.hxya.novel.book.controller;


import ltd.hxya.novel.book.entity.BookCategory;
import ltd.hxya.novel.book.mapper.WordAnalysisMapper;
import ltd.hxya.novel.book.service.IBookCategoryService;
import ltd.hxya.novel.book.service.impl.WordAnalysisServiceImpl;
import ltd.hxya.novel.book.vo.BookCategoryVo;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.entity.rowdata.WordAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 小说类别表 前端控制器
 * </p>
 *
 * @author hxya
 * @since 2022-09-13
 */
@RestController
@RequestMapping("/book/bookCategory")
public class BookCategoryController {

    @Autowired
    private IBookCategoryService bookCategoryService;

    @Autowired
    private WordAnalysisServiceImpl wordAnalysisService;

    @Autowired
    private WordAnalysisMapper wordAnalysisMapper;

    /**
     * 查询小说分类,并添加到Redis中
     */
    @GetMapping("/listCategory")
    public Result<List<BookCategoryVo>> listCategory(Integer categoryCondition, Boolean workDirection){
        List<BookCategoryVo> bookCategoryVos = bookCategoryService.listCategory(categoryCondition,workDirection);
        return Result.success(bookCategoryVos);
    }

    //训练分类模型
    @PostMapping("categoryTrain")
    public Result modalTrain(@RequestBody WordAnalysis wordAnalysis) throws IOException {

        wordAnalysisService.trainData(wordAnalysis.getCategory(), wordAnalysis.getText());

        return Result.success();
    }


    @GetMapping("/getCategory")
    public Result<String> getCategory(String desc) throws IOException {
        String category = wordAnalysisService.calculateMaxSimilarCategory(desc);
        return Result.success(category);
    }

    @GetMapping("categoryList")
    public Result<List<String>> categoryList() {
        List<WordAnalysis> wordAnalysisList = wordAnalysisService.getBaseMapper().selectList(null);
        List<String> collect = wordAnalysisMapper.getCategory().stream().filter((item -> {
            if ("value".equals(item)) {
                return false;
            }
            return true;
        })).collect(Collectors.toList());
        return Result.success(collect);
    }


    /**
     * 修改小说分类
     */
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody BookCategory bookCategory){
        bookCategoryService.updateById(bookCategory);
        return Result.success();
    }

}
